package com.android.readcard.write;

import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.util.Log;

import com.android.readcard.inter.ReadIccidListener;
import com.android.readcard.inter.WriteCardListener;
import com.android.readcard.utils.djlm.DJLMReader3.djlm_xieka.DJLMSimCallBack;
import com.android.readcard.utils.djlm.DJLMReader3.djlm_xieka.DJLMSimTools;
import com.android.readcard.utils.djlm.DJLM_ReadICCIDAndSIM;


/**
 * 店家联盟写卡
 */

public class DjlmWrite {
    private String blueAddress;
    private Activity mActivity;
    private ReadIccidListener mReadIccidListener;
    private WriteCardListener mWriteCardListener;
    private DJLM_ReadICCIDAndSIM djlm_readICCID;
    private int mType;


    public DjlmWrite(Activity context, String blueAddress, int mType, ReadIccidListener readIccidListener
            , WriteCardListener writeCardListener) {
        this.mActivity = context;
        this.blueAddress = blueAddress;
        this.mType = mType;
        this.mReadIccidListener = readIccidListener;
        this.mWriteCardListener = writeCardListener;

    }

    //******************************************读iccid************************************************
    public void connect() {
        mActivity.runOnUiThread(new Runnable() {
            @Override
            public void run() {
                BLEReadOnce(blueAddress);
            }
        });
    }

    private String strAddress;

    private void BLEReadOnce(String strAddress) {
        this.strAddress = strAddress;
        BluetoothAdapter ba = BluetoothAdapter.getDefaultAdapter();
        if (ba.isDiscovering()) {
            ba.cancelDiscovery();
        }
        try {
            DJLMSimTools djlmSimTools = new DJLMSimTools(mActivity, strAddress, new DJLMSimCallBack() {
                @Override
                public void success(int type, String result) {
                    Log.e("项目异常", type + "------DJLMSimTools---success---" + result);
                    if (type == DJLMSimTools.TYPE_ICCID_SIM) {
                        String[] split = result.split("\\|");
                        if (mType == 0) { // 废卡重置
                            if (split[1].contains("FFF")) {
                                mReadIccidListener.readIccidIsChengKa(1617, split[0], "此卡是白卡,无需重置");
                            } else {
                                mReadIccidListener.readIccidSuc(4, split[0], split[1]);
                            }
                        } else { // 补卡,写卡
                            if (!split[1].contains("FFF")) {
                                mReadIccidListener.readIccidIsChengKa(1617, split[0], "此卡是成卡,请换卡重试");
                            } else {
                                mReadIccidListener.readIccidSuc(4, split[0], split[1]);
                            }
                        }
                    } else if (type == DJLMSimTools.TYPE_WRITE_SIM) {
                        mWriteCardListener.writeSuc();
                    }
                }

                @Override
                public void errror(int type, String result) {
                    Log.e("项目异常", type + "------DJLMSimTools----errror--" + result);
                    if (type == DJLMSimTools.TYPE_ICCID_SIM) {
                        mReadIccidListener.readIccidErr("读取ICCID失败," + result);
                    } else if (type == DJLMSimTools.TYPE_WRITE_SIM) {
                        mWriteCardListener.writeErr();
                    }
                }
            });
            djlmSimTools.readICCID_SIM().start();
        } catch (Exception e) {
            Log.e("项目异常", "------DJLMSimTools---Exception---" + e.toString());
            e.printStackTrace();
            mReadIccidListener.readIccidErr("读取ICCID失败,请重试");
        }
    }

    //************************************写卡*******************************
    public void writeCard(String IMSI, String Send_number) {

        try {
            DJLMSimTools djlmSimTools = new DJLMSimTools(mActivity, strAddress, new DJLMSimCallBack() {
                @Override
                public void success(int type, String result) {
                    Log.e("项目异常", type + "------DJLMSimTools---success---" + result);
                    if (type == DJLMSimTools.TYPE_ICCID_SIM) {
                        String[] split = result.split("\\|");
                        if (mType == 0) { // 废卡重置
                            if (split[1].contains("FFF")) {
                                mReadIccidListener.readIccidIsChengKa(1617, split[0], "此卡是白卡,无需重置");
                            } else {
                                mReadIccidListener.readIccidSuc(4, split[0], split[1]);
                            }
                        } else { // 补卡,写卡
                            if (!split[1].contains("FFF")) {
                                mReadIccidListener.readIccidIsChengKa(1617, split[0], "此卡是成卡,请换卡重试");
                            } else {
                                mReadIccidListener.readIccidSuc(4, split[0], split[1]);
                            }
                        }
                    } else if (type == DJLMSimTools.TYPE_WRITE_SIM) {
                        mWriteCardListener.writeSuc();
                    }
                }

                @Override
                public void errror(int type, String result) {
                    Log.e("项目异常", type + "------DJLMSimTools----errror--" + result);
                    if (type == DJLMSimTools.TYPE_ICCID_SIM) {
                        mReadIccidListener.readIccidErr("读取ICCID失败," + result);
                    } else if (type == DJLMSimTools.TYPE_WRITE_SIM) {
                        mWriteCardListener.writeErr();
                    }
                }
            });
            djlmSimTools.writeSIM(IMSI, Send_number).start();

        } catch (Exception e) {
            Log.e("项目异常", "------DJLMSimTools---Exception---" + e.toString());
            e.printStackTrace();
            mReadIccidListener.readIccidErr("读取ICCID失败,请重试");
        }

    }

    /**
     * 关闭蓝牙连接
     */
    public void disConnect() {
        if (djlm_readICCID != null) {
            djlm_readICCID.disconnect();
        }
    }
}
